<%@ page import="com.example.util.DBUtil" %>
<%@ page import="com.example.domain.User" %>
<%@ page import="java.sql.*" %>
<%@ page contentType="text/plain;charset=UTF-8" language="java" %>
<%
    // 检查用户是否已登录
    User currentUser = (User) session.getAttribute("user");
    if (currentUser == null) {
        response.getWriter().print("未登录");
        return;
    }
    
    Connection conn = null;
    PreparedStatement deleteStmt = null;
    
    try {
        int productId = Integer.parseInt(request.getParameter("productId"));
        
        conn = DBUtil.getConnection();
        
        // 从购物车中删除商品
        String deleteSql = "DELETE FROM tb_cart WHERE member_id = ? AND product_id = ?";
        deleteStmt = conn.prepareStatement(deleteSql);
        deleteStmt.setInt(1, currentUser.getId());
        deleteStmt.setInt(2, productId);
        
        int rowsAffected = deleteStmt.executeUpdate();
        if (rowsAffected > 0) {
            response.getWriter().print("success");
        } else {
            response.getWriter().print("删除失败");
        }
    } catch (Exception e) {
        response.getWriter().print("错误: " + e.getMessage());
    } finally {
        // 关闭数据库资源
        try {
            if (deleteStmt != null) deleteStmt.close();
            if (conn != null) conn.close();
        } catch (SQLException e) {
            // 忽略关闭连接的异常
        }
    }
%>